package com.java110.mall.system.cmd.admin;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.intf.cart.IStoreOrderInnerServiceSMO;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.DateUtil;
import com.java110.utils.util.ListUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Java110Cmd(serviceCode = "admin.queryStoreOrderDayCount")
public class QueryStoreOrderDayCountCmd extends Cmd {

    @Autowired
    private IStoreOrderInnerServiceSMO storeOrderInnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        if (!reqJson.containsKey("startTime")) {
            String startTime = DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_B);
            reqJson.put("endTime", startTime);
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_MONTH,-7);
            reqJson.put("startTime", DateUtil.getFormatTimeStringB(calendar.getTime()));
        }

        String startTime = reqJson.getString("startTime");
        if (StringUtil.isEmpty(startTime)) {
            startTime = DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_B);
            reqJson.put("endTime", startTime);
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_MONTH,-7);
            reqJson.put("startTime", DateUtil.getFormatTimeStringB(calendar.getTime()));
        }
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {


        List<Map> datas = storeOrderInnerServiceSMOImpl.queryStoreOrderDayCount(reqJson);

        if (ListUtil.isNull(datas)) {
            datas = new ArrayList<>();
            Map data = new HashMap();
            data.put("createTime", DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_B));
            data.put("count", 0);
            datas = new ArrayList<>();
            datas.add(data);

            context.setResponseEntity(ResultVo.createResponseEntity(datas));
            return;
        }


        context.setResponseEntity(ResultVo.createResponseEntity(datas));
    }
}
